import { useEffect, useState } from 'react'
import copy from 'copy-to-clipboard'
import { Message } from '@kedacom/react-next'
export function isValidKey(key: string | number | symbol, object: object): key is keyof typeof object {
	return key in object
}
const useLogDetail = (detailData: any) => {
	useEffect(() => {
		detailChange(detailData)
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, [])
	const [detailLogData, setDetailLogData] = useState([{ label: '', value: '' }])
	const [logRemark, setLogRemark] = useState('')
	const detailChange = (rowData: any) => {
		let list = []
		let changeObject = {
			logid: '日志ID',
			subject_name: '事件主体',
			module: '系统功能模块',
			object_name: '事件客体',
			details: '事件内容',
			result: '事件结果',
			time: '事件时间',
			level: '日志风险级别',
			behaviour_type: '行为类别',
			type: '事件种类',
		}
		let actionList = ['违规行为', '异常行为', '一般行为']
		let levelList = ['紧急', '警报', '关键', '错误', '警告', '通知', '信息', '调试']
		let resultList = ['失败', '成功']
		for (let key in changeObject) {
			let reValue = ''
			if (isValidKey(key, rowData)) {
				if (key === 'result') {
					reValue = resultList[rowData[key]]
				} else if (key === 'type') {
					if (rowData[key] === 'm') {
						reValue = '管理员操作日志'
					} else if (rowData[key] === 'a') {
						reValue = '用户操作日志'
					} else {
						reValue = '其他日志'
					}
				} else if (key === 'level') {
					reValue = levelList[rowData[key]]
				} else if (key === 'behaviour_type') {
					reValue = actionList[rowData[key] - 1]
				} else {
					reValue = rowData[key]
				}
				list.push({ label: changeObject[key], value: reValue })
			}
		}
		if (rowData['reservation']) {
			setLogRemark(rowData['reservation'])
		}

		setDetailLogData([...list])
	}
	const onCopyClick = () => {
		copy(logRemark)
		let m = Message.show({
			context: '备注内容已复制',
			type: 'info',
			align: 'center',
		})
		setTimeout(() => {
			m.hide()
		}, 500)
	}
	return { detailLogData, logRemark, onCopyClick }
}
export default useLogDetail
